package com.cqqs.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cqqs.common.bean.BaseRes;
import com.cqqs.controller.dto.req.project.ProjectPageReq;
import com.cqqs.controller.dto.req.project.ProjectSaveReq;
import com.cqqs.controller.dto.req.project.ProjectUpdateReq;
import com.cqqs.controller.dto.res.project.ProjectDetailRes;
import com.cqqs.controller.dto.res.project.ProjectPageRes;
import com.cqqs.controller.dto.res.project.ProjectPatentsRes;
import com.cqqs.mapper.TechProjectMapper;
import com.cqqs.mapper.po.TechProject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 科技项目管理
 *
 * @author mjxie
 * @date 2022/09/23
 */
@Service
public class TechProjectService {


    private final TechProjectMapper techProjectMapper;

    @Autowired
    public TechProjectService(TechProjectMapper techProjectMapper) {
        this.techProjectMapper = techProjectMapper;
    }

    public void save(ProjectSaveReq projectSaveReq) {
    }

    public void update(ProjectUpdateReq projectUpdateReq) {
    }

    public BaseRes<ProjectDetailRes> getDetail(Integer id) {
        return null;
    }

    /**
     * 批量删除项目信息
     *
     * @param ids
     * @return
     */
    public BaseRes delete(Integer[] ids) {
        return null;
    }


    /**
     * 分页查询项目信息
     *
     * @param projectPageReq
     * @return
     */
    public List<ProjectPageRes> getPage(ProjectPageReq projectPageReq) {

        List<ProjectPageRes> result = new ArrayList<>();
        LambdaQueryWrapper<TechProject> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //项目类型
        Integer type = projectPageReq.getType();
        if (ObjectUtils.isNotNull(type)) {
            lambdaQueryWrapper.eq(TechProject::getType, type);
        }
        String projectName = projectPageReq.getSearchStr();
        if (StringUtils.isNotEmpty(projectName)) {
            lambdaQueryWrapper.like(TechProject::getName, projectName);
        }
        List<TechProject> techProjects = techProjectMapper.selectList(lambdaQueryWrapper);

        if (ObjectUtils.isNotEmpty(techProjects)) {
            techProjects.forEach(techProject -> {
                ProjectPageRes projectPageRes = new ProjectPageRes();
                projectPageRes.setProjectId(techProject.getProjectId());
                projectPageRes.setName(techProject.getName());
                projectPageRes.setSubNo(techProject.getSubNo());
                projectPageRes.setType(techProject.getType());
                projectPageRes.setContributionDept(techProject.getContributionDept());
                projectPageRes.setExecuteDepartment(techProject.getExecuteDepartment());
                //todo 设置关联关系
                projectPageRes.setHasRelation(0);

            });
        }

        return result;
    }

    public BaseRes<ProjectPatentsRes> getPatentsByProjectId(Integer projectId) {
        return null;
    }
}
